package com.example.home.logistics.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.home.R;
import com.example.home.logistics.adapter.LogisticsSearchDetaiAdapter;
import com.example.home.logistics.bean.LogisticsSearchDataBean;
import com.example.home.logistics.bean.LogisticsSearchDetaiBean;
import com.example.home.tool.OKHttpTool;
import com.google.gson.Gson;

import static com.example.home.tool.StaticActi.ip;

public class LogisticsSearchDetailActivity extends AppCompatActivity {
    private ImageView back;
    private ImageView img;
    private TextView name;
    private TextView phone;
    private RecyclerView rcvList;
    private Button btnSend;
    private LogisticsSearchDataBean.DataBean dataBean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_logistics_search_detail);
        Intent intent = getIntent();
        String seaid = intent.getStringExtra("seaid");
        Gson gson = new Gson();
        dataBean = gson.fromJson(seaid, LogisticsSearchDataBean.DataBean.class);
        initView();
        getData(dataBean);
    }



    private void getData(LogisticsSearchDataBean.DataBean dataBean) {
        OKHttpTool.doNetRequest("prod-api/api/logistics-inquiry/logistics_info/" + dataBean.getInfoNo(),
                OKHttpTool.GET,
                LogisticsSearchDetaiBean.class,
                new OKHttpTool.NetCallback() {
                    @Override
                    public void onSuccess(Object obj) {
                        LogisticsSearchDetaiBean logisticsSearchDetaiBean = (LogisticsSearchDetaiBean) obj;
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                bindData(logisticsSearchDetaiBean);
                            }
                        });
                    }

                    @Override
                    public void onFailure(String msg) {

                    }
                }
        );
    }

    private void bindData(LogisticsSearchDetaiBean logisticsSearchDetaiBean) {
        if (logisticsSearchDetaiBean != null && logisticsSearchDetaiBean.getData() != null) {
            LogisticsSearchDetaiAdapter logisticsSearchDetaiAdapter = new LogisticsSearchDetaiAdapter(this, logisticsSearchDetaiBean.getData().getStepList());
            LinearLayoutManager linearLayoutManager = new LinearLayoutManager(LogisticsSearchDetailActivity.this);
            rcvList.setAdapter(logisticsSearchDetaiAdapter);
            rcvList.setLayoutManager(linearLayoutManager);
        } else {
            // Handle the case where the response or its data is null
        }
    }


    private void initView() {
        back = findViewById(R.id.back);
        img = findViewById(R.id.img);
        name = findViewById(R.id.name);
        phone = findViewById(R.id.phone);
        rcvList = findViewById(R.id.rcv_list);
        btnSend = findViewById(R.id.btn_send);

        Glide.with(LogisticsSearchDetailActivity.this).load(ip+dataBean.getCompany().getImgUrl()).into(img);
        name.setText(dataBean.getCompany().getName());
        phone.setText(dataBean.getCompany().getPhone());

        btnSend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(LogisticsSearchDetailActivity.this,ComplaintDetails.class);
                intent.putExtra("companyId",String.valueOf(dataBean.getCompanyId()));
                intent.putExtra("infoNo",String.valueOf(dataBean.getInfoNo()));
                intent.putExtra("name",String.valueOf(dataBean.getCompany().getName()));
                startActivity(intent);
            }
        });


        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                LogisticsSearchDetailActivity.this.finish();
            }
        });
    }
}